/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.noa;

import java.util.HashMap;

import ag.ion.bion.officelayer.application.IOfficeApplication;
import ag.ion.bion.officelayer.application.OfficeApplicationException;
import ag.ion.bion.officelayer.application.OfficeApplicationRuntime;
import ag.ion.bion.officelayer.document.DocumentException;
import ag.ion.bion.officelayer.document.IDocument;
import ag.ion.bion.officelayer.document.IDocumentService;
import ag.ion.bion.officelayer.filter.MSOffice97Filter;
import ag.ion.bion.officelayer.filter.PDFFilter;
import ag.ion.bion.officelayer.text.ITextDocument;
import ag.ion.bion.officelayer.text.ITextRange;
import ag.ion.noa.NOAException;
import ag.ion.noa.search.ISearchResult;
import ag.ion.noa.search.SearchDescriptor;
import ag.ion.noa.text.TextRangeSelection;

/**
 * @author Michael Nitsche
 * 11.08.2010	Erstellt
 */
public class Noa {

	@SuppressWarnings("unchecked")
	public static IOfficeApplication startOOo(String oooPfad) throws OfficeApplicationException, DocumentException {
		HashMap appConf = new HashMap();
		appConf.put(IOfficeApplication.APPLICATION_HOME_KEY, oooPfad);
		appConf.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);

		IOfficeApplication officeAplication = OfficeApplicationRuntime.getApplication(appConf);
		officeAplication.activate();
		return officeAplication;
	}

	public static void ersetzen(ITextDocument textDocument, String regex, String ersatz) {
		SearchDescriptor searchDescriptor = new SearchDescriptor(regex);
		searchDescriptor.setIsCaseSensitive(false);
		ISearchResult searchResult = textDocument.getSearchService().findAll(searchDescriptor);
		if(!searchResult.isEmpty()) {
			ITextRange[] textRanges = searchResult.getTextRanges();
			for(int i = 0; i < textRanges.length; i++)
				textRanges[i].setText(ersatz);
		}
	}

	public static void suchen(ITextDocument textDocument, String regex) throws NOAException {
		SearchDescriptor searchDescriptor = new SearchDescriptor(regex);
		searchDescriptor.setIsCaseSensitive(true);

		ISearchResult searchResult = textDocument.getSearchService().findFirst(searchDescriptor);
		if(!searchResult.isEmpty()) {
			//Ergebnis markieren
			ITextRange[] textRanges = searchResult.getTextRanges();
			textDocument.setSelection(new TextRangeSelection(textRanges[0]));
		}
	}

	public static IDocument ladeDokument(IDocumentService documentService, String url) throws DocumentException {
		return documentService.loadDocument(url);
	}

	public static void speichernPDF(ITextDocument doc, String ziel) throws DocumentException {
		PDFFilter pdffilter = new PDFFilter();
		doc.getPersistenceService().export(ziel, pdffilter);
	}

	public static void speichernDOC(ITextDocument doc, String ziel) throws DocumentException {
		MSOffice97Filter msofficefilter = new MSOffice97Filter();
		doc.getPersistenceService().export(ziel, msofficefilter);
	}

	public static void schliessen(ITextDocument doc) { //Naja ... mager!
		doc.close();
	}
}
